package com.financing.platform.task;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.financing.mail.dto.MailContentDTO;
import com.financing.mail.service.MailSenderService;
import com.financing.platform.dao.IAdvertDao;
import com.financing.platform.dao.IFaUserDao;
import com.financing.platform.dao.IMessageDao;
import com.financing.platform.dao.IMessageMailDao;
import com.financing.platform.dao.IProductRecommendDao;
import com.financing.platform.dto.AdvertDTO;
import com.financing.platform.dto.MessageDTO;
import com.financing.platform.dto.MessageMailDTO;
import com.financing.platform.service.IEmailTemplateService;
import com.financing.platform.util.Dict;
import com.financing.platform.util.StringUtil;
import com.financing.platform.vo.EmailTemplateProjectVO;

/**
 * 
* @ClassName: FaVerifyMailJob 
* @Description: 
* @author chenming 
* @date 2014年9月22日 下午6:32:23 
*
 */
@Component(value="newProjectRecommendMailJob" )
public class NewProjectRecommendMailJob {
	
	Logger log = Logger.getLogger(this.getClass());
	
	@Autowired
	private IFaUserDao faUserDaoImpl;
	
	@Resource
	private MailSenderService mailSenderService;
	
	@Autowired
	private IEmailTemplateService emailTemplateServiceImpl;
	
	@Autowired
	private IProductRecommendDao productRecommendDaoImpl;
	
	@Autowired
	private IMessageMailDao messageMailDaoImpl;
	
	@Autowired
	private IMessageDao messageDaoImpl;
	

	@Autowired
	private IAdvertDao advertDaoImpl;
	
//	@Scheduled(cron = "0 0 09 ? * 1") 
	public void faVerifyNeed(){
		try{
			String imageUrl = "";
			String hrefUrl = "";
			AdvertDTO advertDTO = new AdvertDTO();
			advertDTO.setAdvertPosition(Dict.ad_mail);
			List<AdvertDTO> listAd = advertDaoImpl.queryAdvertList(advertDTO);
			if(listAd != null && listAd.size()>0){
				advertDTO = listAd.get(0);
				imageUrl = advertDTO.getPicAddre();
				hrefUrl = advertDTO.getLinkAddre();
			}
			
			Integer begin = 0 ;
			Integer size = 500;
			//循环查询认证通过的FA信息，发送邮件 每次500个
			while(true){
				Map<String,Integer> param = new HashMap<String,Integer>();
				param.put("begin", begin);
				param.put("size", size);
				List<String> list = faUserDaoImpl.queryFaUserVPassEmail(param);
				if(list == null || list.size()<=0){
					break;
				}
				for(int i = 0;i < list.size(); i++){
					String emailAddr = list.get(i);
					
					//邮件地址不为空就发送邮件
					if(!StringUtil.isNullOrEmptyStr(emailAddr)){
						List<EmailTemplateProjectVO> projectList = productRecommendDaoImpl.getEmailTemplateProjectList(4);
						projectList.addAll(productRecommendDaoImpl.queryEmailProjectList(16));
						Map<String,Object> emailParam = new HashMap<String,Object>();
						emailParam.put("list", projectList);
						emailParam.put("imageUrl", imageUrl);
						emailParam.put("hrefUrl", hrefUrl);
						emailParam = emailTemplateServiceImpl.spliceEmailContent(Dict.mail_template_product_publish, emailParam);
						if(!emailParam.isEmpty() && emailParam.size() >0){
							MessageDTO messageDTO = new MessageDTO();
							MessageMailDTO messageMailDTO = new MessageMailDTO();
							messageDTO.setMsgTitle((String)emailParam.get(Dict.mail_subject));
							messageDTO.setMsgContent((String)emailParam.get(Dict.mail_content));
							messageDTO.setMsgType(Dict.message_type_project);
							messageDTO.setStatus(Dict.status_normal);
							messageDTO.setCreateBy("system");
							messageDTO.setCreateDate(new Date());
							messageDTO.setUpdateBy("system");
							messageDTO.setUpdateDate(new Date());;
							messageMailDTO.setFpMailMessageId(messageDaoImpl.addMessage(messageDTO));
							messageMailDTO.setFpMailContent((String)emailParam.get(Dict.mail_content));
							messageMailDTO.setFpMailTitle((String)emailParam.get(Dict.mail_subject));
							messageMailDTO.setFpMailType(Dict.mail_product_publish);
							messageMailDTO.setFpMailSendType(Dict.message_type_now);
							messageMailDTO.setFpMailSendTimes(new Integer(0));
							messageMailDTO.setFpMailTo(emailAddr);
							messageMailDTO.setStatus(Dict.status_normal);
							messageMailDTO.setFpMailSendStatus(Dict.message_status_not_send);
							messageMailDTO.setCreateBy(messageDTO.getUpdateBy());
							messageMailDTO.setUpdateBy(messageDTO.getUpdateBy());
							messageMailDTO.setCreateDate(messageDTO.getUpdateDate());
							messageMailDTO.setUpdateDate(messageDTO.getUpdateDate());
							messageMailDaoImpl.addMessageMail(messageMailDTO);
						}
					}
				}
				begin = begin + size;
			}
		}catch(Exception e){
			log.error("product publish Mail error ",e);
			MailContentDTO mail = new MailContentDTO();
			mail.setRecAddress("chenming@superfa.cn");
			mail.setSubject("新产品推荐邮件异常");
			mail.setContext("新产品推荐邮件异常"+e.getMessage()+e);
			mailSenderService.send(mail);
		}
		
		
	}
	
	
}
